#include "logger.h"
//组装时产生大问题
Logger::Logger()
{
    file.setFileName("log.txt");
}

bool Logger::isCreated()
{
    if(!file.open(QIODevice::WriteOnly | QFile::Append | QFile::Text)){
        QMessageBox::critical(nullptr, "提示", "无法创建记录文件", QMessageBox::Ok);
        qDebug() << "can not create file";
        return false;
    }
    else {
        return true;
    }
}

void Logger::writeLog(int log)
{
    switch (log) {
    case 0:
        if(this->isCreated()){
            QTextStream in(&file);
            in <<  time.currentDateTime().toString() << "\t" << "Cannot login" << endl;
            file.close();
        }
        break;
    case 1:
        if(this->isCreated()){
            QTextStream in(&file);
            in
                    << time.currentDateTime().toString()
                    << "\t"
                    << "Cannot connect to database"
                    << endl;
            file.close();
        }
        break;
    case 2:
        if(this->isCreated()){
            QTextStream in(&file);
            qDebug() << "login";
            in
                    << time.currentDateTime().toString()
                    << "\t"
                    << "Login successfully as:";
            for(int i = 0;i < usrname.length(); ++i)
            {

                    in << usrname[i];
            }
                    in << QString(" @%1:%2")
                       .arg(host)
                       .arg(port)
                    << endl;
            file.close();
        }
        break;
    case 3:
        if(this->isCreated()){
            QTextStream in(&file);
            in
                    << time.currentDateTime().toString()
                    << "\t"
                    <<"Wrong username or password"
                    << endl;
            file.close();
        }
        break;
    case 4:
        if(this->isCreated()){
            QTextStream in(&file);
            in
                    << time.currentDateTime().toString()
                    << "\t"
                    << "IP set to: "
                    << this->host
                    << " and port set to: "
                    << this->port
                    << endl;
            file.close();
        }
        break;
    case 5:
        if(this->isCreated()){
            QTextStream in(&file);
            in
                    << time.currentDateTime().toString()
                    << "\t"
                    << "Download "
                    << fileName
                    <<" successfully"
                    << endl;
            file.close();
        }
        break;
    case 6:
        if(this->isCreated()){
            QTextStream in(&file);
            in
                    << time.currentDateTime().toString()
                    << "\t"
                    << "Tried to download "
                    << fileName
                    << ", but faild"
                    << endl;
            file.close();
        }
        break;
    case 7:
        if(this->isCreated()){
            QTextStream in(&file);
            in
                    << time.currentDateTime().toString()
                    << "\t"
                    << "Faild to connect to host"
                    << endl;
            file.close();
        }
        break;
    case 8:
        if(this->isCreated()){
            QTextStream in(&file);
            in
                    << time.currentDateTime().toString()
                    << "\t"
                    << "Faild to get file info"
                    << endl;
            file.close();
        }
        break;
    case 9:
        if(this->isCreated()){
            QTextStream in(&file);
            in
                    << time.currentDateTime().toString()
                    << "\t"
                    << "User: " + usrname[0] + " skip to step " + fileName.left(fileName.lastIndexOf(".")) + " forcely for some reason"
                    << endl;
            file.close();
        }
        break;
    case 10:
        if(this->isCreated()){
            QTextStream in(&file);
            in
                    << time.currentDateTime().toString()
                    << "\t"
                    << "User: " + usrname[0] + " reassembled " + fileName.left(fileName.lastIndexOf("."))
                    << endl;
            file.close();
        }
        break;
    case 11:
        if(this->isCreated()){
            QTextStream in(&file);
            in
                    << time.currentDateTime().toString()
                    << "\t"
                    << "User: " + usrname[0] + "successfully assembled " + fileName.left(fileName.lastIndexOf(".")) + ", go to next step"
                    << endl;
            file.close();
        }
        break;
    case 12:
        if(this->isCreated()){
            QTextStream in(&file);
            in
                    << time.currentDateTime().toString()
                    << "\t"
                    << "Logout Successfully"
                    << endl;
            file.close();
        }
        break;
    case 13:
        if(this->isCreated()){
            QTextStream in(&file);
            in
                    << time.currentDateTime().toString()
                    << "\t"
                    << "Update database faild"
                    << endl;
            file.close();
        }
        break;
    default:
        break;
    }
}

void Logger::setIpAndPort(QString host, unsigned short port)
{
    this->host = host;
    this->port = port;
}

void Logger::setFileName(QString fileName)
{
    this->fileName = fileName;
}

void Logger::setUsrname(QStringList usrname)
{
    this->usrname = usrname;

}
